/*
opensl_example.c:
OpenSL example module 
Copyright (c) 2012, Victor Lazzarini
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
 * Neither the name of the <organization> nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <android/log.h>
#include "opensl_io.h"

#define BUFFERFRAMES 1024
#define VECSAMPS_MONO 64
#define VECSAMPS_STEREO 128
#define SR 44100





#define INDEX19K8DB 253
#define INDEX19K0DB 123

#define INDEX20K8DB 86
#define INDEX20K0DB 258

#define INDEX21K8DB 21
#define INDEX21K0DB 21

static int on;
void start_process() {

	OPENSL_STREAM  *p; // typedefs structure

	int samps, i, j;

	int nRawDataIndex=0;

	float  inbuffer[VECSAMPS_MONO], outbuffer[VECSAMPS_STEREO]; // declare buffer  mono?? stereo??

	p = android_OpenAudioDevice(SR,1,2,BUFFERFRAMES); // params : sampling rate, inchannels, outchannels, bufferframes

	if(p == NULL) return;

	on = 1;


	float table19k8DB[INDEX19K8DB]={
			0.0375061035156250,
			-0.200744628906250,
			0.326690673828125,
			-0.391815185546875,
			0.384216308593750,
			-0.305175781250000,
			0.169342041015625,
			-0.00213623046875000,
			-0.165527343750000,
			0.302459716796875,
			-0.383117675781250,
			0.392547607421875,
			-0.329071044921875,
			0.204406738281250,
			-0.0417480468750000,
			-0.128723144531250,
			0.275207519531250,
			-0.370574951171875,
			0.397094726562500,
			-0.349761962890625,
			0.237335205078125,
			-0.0809020996093750,
			-0.0906372070312500,
			0.245330810546875,
			-0.354400634765625,
			0.397613525390625,
			-0.366851806640625,
			0.268035888671875,
			-0.119262695312500,
			-0.0516357421875000,
			0.212982177734375,
			-0.334686279296875,
			0.394256591796875,
			-0.380432128906250,
			0.296051025390625,
			-0.156494140625000,
			-0.0121154785156250,
			0.178405761718750,
			-0.311676025390625,
			0.386840820312500,
			-0.390228271484375,
			0.321044921875000,
			-0.192199707031250,
			0.0275573730468750,
			0.142150878906250,
			-0.285461425781250,
			0.375701904296875,
			-0.396240234375000,
			0.342926025390625,
			-0.225921630859375,
			0.0669860839843750,
			0.104431152343750,
			-0.256469726562500,
			0.360748291015625,
			-0.398132324218750,
			0.361389160156250,
			-0.257446289062500,
			0.105743408203125,
			0.0657043457031250,
			-0.224853515625000,
			0.342254638671875,
			-0.396057128906250,
			0.376220703125000,
			-0.286407470703125,
			0.143493652343750,
			0.0262451171875000,
			-0.191009521484375,
			0.320312500000000,
			-0.390014648437500,
			0.387329101562500,
			-0.312561035156250,
			0.179718017578125,
			-0.0134887695312500,
			-0.155334472656250,
			0.295196533203125,
			-0.380279541015625,
			0.394500732421875,
			-0.335601806640625,
			0.214233398437500,
			-0.0530700683593750,
			-0.118041992187500,
			0.267089843750000,
			-0.366607666015625,
			0.397857666015625,
			-0.355224609375000,
			0.246551513671875,
			-0.0920410156250000,
			-0.0795288085937500,
			0.236419677734375,
			-0.349304199218750,
			0.397277832031250,
			-0.371429443359375,
			0.276519775390625,
			-0.130157470703125,
			-0.0403747558593750,
			0.203369140625000,
			-0.328521728515625,
			0.392700195312500,
			-0.383880615234375,
			0.303649902343750,
			-0.166992187500000,
			-0.000701904296875000,
			0.168212890625000,
			-0.304534912109375,
			0.384216308593750,
			-0.392517089843750,
			0.327789306640625,
			-0.202239990234375,
			0.0390319824218750,
			0.131408691406250,
			-0.277435302734375,
			0.371887207031250,
			-0.397247314453125,
			0.348724365234375,
			-0.235443115234375,
			0.0783081054687500,
			0.0932617187500000,
			-0.247558593750000,
			0.355865478515625,
			-0.397949218750000,
			0.366088867187500,
			-0.266235351562500,
			0.116821289062500,
			0.0542602539062500,
			-0.215148925781250,
			0.336303710937500,
			-0.394744873046875,
			0.379852294921875,
			-0.294372558593750,
			0.154174804687500,
			0.0146484375000000,
			-0.180755615234375,
			0.313293457031250,
			-0.387542724609375,
			0.389770507812500,
			-0.319610595703125,
			0.189880371093750,
			-0.0250549316406250,
			-0.144592285156250,
			0.287261962890625,
			-0.376617431640625,
			0.395904541015625,
			-0.341674804687500,
			0.223785400390625,
			-0.0644531250000000,
			-0.106994628906250,
			0.258453369140625,
			-0.361846923828125,
			0.398040771484375,
			-0.360229492187500,
			0.255493164062500,
			-0.103118896484375,
			-0.0683288574218750,
			0.227081298828125,
			-0.343627929687500,
			0.396301269531250,
			-0.375274658203125,
			0.284484863281250,
			-0.140808105468750,
			-0.0289916992187500,
			0.193481445312500,
			-0.321960449218750,
			0.390594482421875,
			-0.386566162109375,
			0.310791015625000,
			-0.177215576171875,
			0.0105895996093750,
			0.157867431640625,
			-0.297088623046875,
			0.381011962890625,
			-0.394165039062500,
			0.333984375000000,
			-0.211761474609375,
			0.0502319335937500,
			0.120666503906250,
			-0.269256591796875,
			0.367614746093750,
			-0.397766113281250,
			0.353912353515625,
			-0.244293212890625,
			0.0892028808593750,
			0.0823669433593750,
			-0.238616943359375,
			0.350585937500000,
			-0.397338867187500,
			0.370239257812500,
			-0.274291992187500,
			0.127471923828125,
			0.0431823730468750,
			-0.205657958984375,
			0.330017089843750,
			-0.393035888671875,
			0.383026123046875,
			-0.301666259765625,
			0.164459228515625,
			0.00341796875000000,
			-0.170593261718750,
			0.306121826171875,
			-0.384765625000000,
			0.391754150390625,
			-0.326080322265625,
			0.199676513671875,
			-0.0362548828125000,
			-0.133941650390625,
			0.279235839843750,
			-0.372650146484375,
			0.396697998046875,
			-0.347198486328125,
			0.232940673828125,
			-0.0756225585937500,
			-0.0959472656250000,
			0.249633789062500,
			-0.356842041015625,
			0.397735595703125,
			-0.364776611328125,
			0.263946533203125,
			-0.114013671875000,
			-0.0570373535156250,
			0.217559814453125,
			-0.337585449218750,
			0.394866943359375,
			-0.378784179687500,
			0.292266845703125,
			-0.151428222656250,
			-0.0175781250000000,
			0.183380126953125,
			-0.314941406250000,
			0.388122558593750,
			-0.389068603515625,
			0.317687988281250,
			-0.187316894531250,
			0.0220947265625000,
			0.147247314453125,
			-0.289276123046875,
			0.377441406250000,
			-0.395507812500000,
			0.339965820312500,
			-0.221374511718750,
			0.0615539550781250,
			0.109710693359375,
			-0.260620117187500,
			0.363006591796875,
			-0.397979736328125,
			0.358978271484375,
			-0.253173828125000,
			0.100402832031250,
			0.0711059570312500,
			-0.229309082031250,
			0.345001220703125,
			-0.396453857421875,
			0.374359130859375,
			-0.282531738281250,
			0.138275146484375


	};








	float table19k0DB[INDEX19K0DB]={

			-0.999969482421875,
			0.908325195312500,
			-0.647583007812500,
			0.266479492187500,
			0.164337158203125,
			-0.564422607421875,
			0.859619140625000,
			-0.994995117187500,
			0.945495605468750,
			-0.720184326171875,
			0.360961914062500,
			0.0653381347656250,
			-0.479553222656250,
			0.804412841796875,
			-0.979949951171875,
			0.973114013671875,
			-0.785644531250000,
			0.451934814453125,
			-0.0342407226562500,
			-0.389770507812500,
			0.741363525390625,
			-0.955108642578125,
			0.991302490234375,
			-0.843200683593750,
			0.538360595703125,
			-0.133483886718750,
			-0.296112060546875,
			0.670898437500000,
			-0.920715332031250,
			0.999511718750000,
			-0.892456054687500,
			0.619506835937500,
			-0.231506347656250,
			-0.199615478515625,
			0.593627929687500,
			-0.877258300781250,
			0.997863769531250,
			-0.932983398437500,
			0.694671630859375,
			-0.327270507812500,
			-0.100921630859375,
			0.510406494140625,
			-0.824890136718750,
			0.986114501953125,
			-0.964141845703125,
			0.762878417968750,
			-0.419738769531250,
			-0.00131225585937500,
			0.422058105468750,
			-0.764465332031250,
			0.964843750000000,
			-0.985778808593750,
			0.823516845703125,
			-0.508270263671875,
			0.0984497070312500,
			0.329681396484375,
			-0.696441650390625,
			0.933837890625000,
			-0.997467041015625,
			0.875915527343750,
			-0.591491699218750,
			0.197021484375000,
			0.234008789062500,
			-0.621490478515625,
			0.893615722656250,
			-0.999450683593750,
			0.919647216796875,
			-0.668884277343750,
			0.293670654296875,
			0.136016845703125,
			-0.540374755859375,
			0.844329833984375,
			-0.991424560546875,
			0.954132080078125,
			-0.739562988281250,
			0.387390136718750,
			0.0367126464843750,
			-0.453979492187500,
			0.786865234375000,
			-0.973449707031250,
			0.979156494140625,
			-0.802825927734375,
			0.477203369140625,
			-0.0630187988281250,
			-0.363067626953125,
			0.721496582031250,
			-0.945892333984375,
			0.994415283203125,
			-0.858123779296875,
			0.562286376953125,
			-0.161895751953125,
			-0.268524169921875,
			0.649078369140625,
			-0.908935546875000,
			0.999847412109375,
			-0.904754638671875,
			0.641723632812500,
			-0.259155273437500,
			-0.171386718750000,
			0.570129394531250,
			-0.862976074218750,
			0.995391845703125,
			-0.942687988281250,
			0.714782714843750,
			-0.354095458984375,
			-0.0725402832031250,
			0.485473632812500,
			-0.808349609375000,
			0.980926513671875,
			-0.971221923828125,
			0.780792236328125,
			-0.445404052734375,
			0.0272521972656250,
			0.396057128906250,
			-0.745880126953125,
			0.956878662109375,
			-0.990112304687500,
			0.839172363281250,
			-0.532440185546875,
			0.126556396484375,
			0.302734375000000,
			-0.675781250000000,
			0.923156738281250,
	};






	float table20k8DB[INDEX20K8DB]={
			0.398040771484375,
			-0.381713867187500,
			0.332916259765625,
			-0.256225585937500,
			0.157623291015625,
			-0.0456542968750000,
			-0.0701599121093750,
			0.180053710937500,
			-0.274627685546875,
			0.346038818359375,
			-0.388092041015625,
			0.397338867187500,
			-0.372863769531250,
			0.316802978515625,
			-0.233917236328125,
			0.131195068359375,
			-0.0173339843750000,
			-0.0979003906250000,
			0.204986572265625,
			-0.294555664062500,
			0.359252929687500,
			-0.393554687500000,
			0.394378662109375,
			-0.361907958984375,
			0.298767089843750,
			-0.210357666015625,
			0.103973388671875,
			0.0110168457031250,
			-0.125244140625000,
			0.228759765625000,
			-0.312927246093750,
			0.370574951171875,
			-0.396850585937500,
			0.389495849609375,
			-0.349151611328125,
			0.279357910156250,
			-0.185760498046875,
			0.0764465332031250,
			0.0393676757812500,
			-0.151794433593750,
			0.251342773437500,
			-0.329650878906250,
			0.380065917968750,
			-0.398223876953125,
			0.382659912109375,
			-0.334686279296875,
			0.258422851562500,
			-0.160125732421875,
			0.0484008789062500,
			0.0675048828125000,
			-0.177673339843750,
			0.272705078125000,
			-0.344757080078125,
			0.387481689453125,
			-0.397491455078125,
			0.373718261718750,
			-0.318359375000000,
			0.236022949218750,
			-0.133758544921875,
			0.0201110839843750,
			0.0952758789062500,
			-0.202545166015625,
			0.292633056640625,
			-0.358032226562500,
			0.393005371093750,
			-0.394744873046875,
			0.363006591796875,
			-0.300720214843750,
			0.212799072265625,
			-0.106903076171875,
			-0.00811767578125000,
			0.122497558593750,
			-0.226348876953125,
			0.311126708984375,
			-0.369506835937500,
			0.396636962890625,
			-0.390136718750000,
			0.350585937500000,
			-0.281311035156250,
			0.188293457031250,
			-0.0792846679687500,
			-0.0364379882812500,
			0.149139404296875,
			-0.249084472656250,
			0.327972412109375,
			-0.379058837890625,

	};





	  float table20k0DB[INDEX20K0DB]={

			  0.999969482421875,
			  -0.951416015625000,
			  0.822082519531250,
			  -0.623077392578125,
			  0.371307373046875,
			  -0.0880432128906250,
			  -0.202667236328125,
			  0.476196289062500,
			  -0.709228515625000,
			  0.882324218750000,
			  -0.980651855468750,
			  0.996032714843750,
			  -0.926879882812500,
			  0.779296875000000,
			  -0.565673828125000,
			  0.304321289062500,
			  -0.0171813964843750,
			  -0.271453857421875,
			  0.537078857421875,
			  -0.757232666015625,
			  0.913085937500000,
			  -0.991790771484375,
			  0.986511230468750,
			  -0.897583007812500,
			  0.732727050781250,
			  -0.505737304687500,
			  0.235961914062500,
			  0.0537719726562500,
			  -0.338989257812500,
			  0.595367431640625,
			  -0.801483154296875,
			  0.939514160156250,
			  -0.998474121093750,
			  0.972503662109375,
			  -0.864013671875000,
			  0.682495117187500,
			  -0.443054199218750,
			  0.166137695312500,
			  0.124877929687500,
			  -0.405364990234375,
			  0.651428222656250,
			  -0.842346191406250,
			  0.961914062500000,
			  -0.999969482421875,
			  0.953521728515625,
			  -0.826080322265625,
			  0.628784179687500,
			  -0.378417968750000,
			  0.0957641601562500,
			  0.194946289062500,
			  -0.469146728515625,
			  0.703613281250000,
			  -0.878387451171875,
			  0.978820800781250,
			  -0.996398925781250,
			  0.929504394531250,
			  -0.783935546875000,
			  0.571838378906250,
			  -0.311462402343750,
			  0.0245361328125000,
			  0.264373779296875,
			  -0.530914306640625,
			  0.752410888671875,
			  -0.910186767578125,
			  0.991302490234375,
			  -0.987945556640625,
			  0.901000976562500,
			  -0.737609863281250,
			  0.511840820312500,
			  -0.242675781250000,
			  -0.0470275878906250,
			  0.332855224609375,
			  -0.590362548828125,
			  0.797912597656250,
			  -0.937805175781250,
			  0.998413085937500,
			  -0.974395751953125,
			  0.867767333984375,
			  -0.687652587890625,
			  0.449523925781250,
			  -0.173126220703125,
			  -0.117858886718750,
			  0.398803710937500,
			  -0.646118164062500,
			  0.838409423828125,
			  -0.959991455078125,
			  0.999969482421875,
			  -0.955596923828125,
			  0.830108642578125,
			  -0.634246826171875,
			  0.384674072265625,
			  -0.102600097656250,
			  -0.188232421875000,
			  0.463073730468750,
			  -0.698760986328125,
			  0.875061035156250,
			  -0.977722167968750,
			  0.997131347656250,
			  -0.932159423828125,
			  0.788177490234375,
			  -0.577362060546875,
			  0.317657470703125,
			  -0.0310668945312500,
			  -0.258209228515625,
			  0.525543212890625,
			  -0.748321533203125,
			  0.907775878906250,
			  -0.990447998046875,
			  0.989105224609375,
			  -0.903900146484375,
			  0.742309570312500,
			  -0.517974853515625,
			  0.249511718750000,
			  0.0399169921875000,
			  -0.326049804687500,
			  0.584503173828125,
			  -0.793457031250000,
			  0.935241699218750,
			  -0.997802734375000,
			  0.975799560546875,
			  -0.871246337890625,
			  0.692749023437500,
			  -0.455718994140625,
			  0.179992675781250,
			  0.110870361328125,
			  -0.392517089843750,
			  0.640747070312500,
			  -0.834655761718750,
			  0.958282470703125,
			  -0.999969482421875,
			  0.957702636718750,
			  -0.833953857421875,
			  0.639526367187500,
			  -0.390899658203125,
			  0.109252929687500,
			  0.181762695312500,
			  -0.457214355468750,
			  0.694061279296875,
			  -0.872009277343750,
			  0.976287841796875,
			  -0.997741699218750,
			  0.934692382812500,
			  -0.792449951171875,
			  0.583404541015625,
			  -0.324707031250000,
			  0.0385437011718750,
			  0.250915527343750,
			  -0.519104003906250,
			  0.743164062500000,
			  -0.904541015625000,
			  0.989135742187500,
			  -0.990020751953125,
			  0.907012939453125,
			  -0.747100830078125,
			  0.523956298828125,
			  -0.256530761718750,
			  -0.0327148437500000,
			  0.319213867187500,
			  -0.578582763671875,
			  0.788909912109375,
			  -0.932739257812500,
			  0.997283935546875,
			  -0.977294921875000,
			  0.874542236328125,
			  -0.697631835937500,
			  0.461761474609375,
			  -0.186645507812500,
			  -0.104248046875000,
			  0.386260986328125,
			  -0.635498046875000,
			  0.831024169921875,
			  -0.956085205078125,
			  0.999969482421875,
			  -0.959594726562500,
			  0.837707519531250,
			  -0.645202636718750,
			  0.397613525390625,
			  -0.116607666015625,
			  -0.174407958984375,
			  0.450622558593750,
			  -0.688537597656250,
			  0.868194580078125,
			  -0.974395751953125,
			  0.997985839843750,
			  -0.937103271484375,
			  0.796783447265625,
			  -0.589050292968750,
			  0.331329345703125,
			  -0.0457153320312500,
			  -0.243896484375000,
			  0.512786865234375,
			  -0.738189697265625,
			  0.901397705078125,
			  -0.987915039062500,
			  0.990814208984375,
			  -0.909667968750000,
			  0.751525878906250,
			  -0.529663085937500,
			  0.263000488281250,
			  0.0259704589843750,
			  -0.312744140625000,
			  0.572998046875000,
			  -0.784667968750000,
			  0.930053710937500,
			  -0.996551513671875,
			  0.978637695312500,
			  -0.877807617187500,
			  0.703033447265625,
			  -0.468261718750000,
			  0.193969726562500,
			  0.0967407226562500,
			  -0.379364013671875,
			  0.629608154296875,
			  -0.826812744140625,
			  0.953857421875000,
			  -1,
			  0.961669921875000,
			  -0.841796875000000,
			  0.650512695312500,
			  -0.404235839843750,
			  0.123718261718750,
			  0.167327880859375,
			  -0.444213867187500,
			  0.683319091796875,
			  -0.864959716796875,
			  0.972991943359375,
			  -0.998565673828125,
			  0.939575195312500,
			  -0.801025390625000,
			  0.594635009765625,
			  -0.337799072265625,
			  0.0523681640625000,
			  0.237426757812500,
			  -0.507110595703125,
			  0.733856201171875,
			  -0.898590087890625,
			  0.987091064453125,
			  -0.991882324218750,
			  0.912872314453125,
			  -0.756744384765625,
			  0.536193847656250,
			  -0.270355224609375,
			  -0.0184020996093750,
			  0.305633544921875,
			  -0.566833496093750,
			  0.780212402343750,
			  -0.927429199218750,
			  0.996063232421875,
			  -0.980407714843750,
			  0.881530761718750,
			  -0.708160400390625,
			  0.474700927734375,
			  -0.201141357421875,
			  -0.0895080566406250,
			  0.372528076171875,
			  -0.624023437500000,
			  0.822845458984375,
			  -0.951660156250000


	  };


	float table21k8DB[INDEX21K8DB]={
	0.0702514648437500,
	-0.0111083984375000,
	-0.0482788085937500,
	0.106628417968750,
	-0.162628173828125,
	0.214935302734375,
	-0.262451171875000,
	0.304077148437500,
	-0.338958740234375,
	0.366302490234375,
	-0.385406494140625,
	0.395996093750000,
	-0.397583007812500,
	0.390380859375000,
	-0.374389648437500,
	0.350158691406250,
	-0.317962646484375,
	0.278808593750000,
	-0.233276367187500,
	0.182617187500000,
	-0.127868652343750 };



	float table21k0DB[INDEX21K0DB]={
	0.994537353515625,
	-0.998779296875000,
	0.980865478515625,
	-0.940765380859375,
	0.879730224609375,
	-0.799102783203125,
	0.700714111328125,
	-0.586578369140625,
	0.459167480468750,
	-0.321807861328125,
	0.177093505859375,
	-0.0284423828125000,
	-0.120941162109375,
	0.267425537109375,
	-0.408081054687500,
	0.539489746093750,
	-0.658996582031250,
	0.763702392578125,
	-0.851440429687500,
	0.920074462890625,
	-0.968200683593750,

	};















	while(on) {

		samps = android_AudioIn(p,inbuffer,VECSAMPS_MONO);

		//for(i = 0, j=0; i < samps; i++, j+=2)
		//	outbuffer[j] = outbuffer[j+1] = inbuffer[i];
		for(i = 0, j=0; i < samps; i++, j+=2){

				outbuffer[j] = outbuffer[j+1] = table20k0DB[nRawDataIndex % INDEX20K0DB];


				nRawDataIndex++;  // for indexing : samps is 64
				if( nRawDataIndex == INDEX20K0DB )
					nRawDataIndex = 0;

		}
		android_AudioOut(p,outbuffer,samps*2);
		__android_log_print(ANDROID_LOG_INFO,"MyTag","%d",samps);
		//LOGI("%d",samps);

	}

	android_CloseAudioDevice(p);
}

void stop_process(){
	on = 0;
}
